<?php
/**
 * File to handle all API requests
 * Accepts POST
 *
 * Each request will be identified by TAG
 * Response will be JSON data

/*
 * check for POST request
 */

if (isset($_POST['tag']) && $_POST['tag'] != '') {
    // get tag
    $tag = $_POST['tag'];
    // include db handler
    require_once 'DB_Functions.php';
    $db = new DB_Functions();

    // response Array
    $response = array("tag" => $tag, "success" => 0, "error" => 0);

    // check for tag type
    if ($tag == 'login') {
        // Request type is check Login
        $username = $_POST['username'];
        $password = $_POST['password'];

        // check for user
        $user = $db->getUser($username, $password);
        if ($user != false) {
            // user found
            // echo json with success = 1
            $response["success"] = 1;
            $response["user"]["pid"] = $user["pid"];
            $response["user"]["name"] = $user["name"];
            echo json_encode($response);
        } else {
            // user not found
            // echo json with error = 1
            $response["error"] = 1;
            $response["error_msg"] = "Incorrect email or password!";
            echo json_encode($response);
        }
    } else if ($tag == 'patientData') {
        $pid = $_POST['pid'];
        
        $user = $db->getPatientData($pid);
        $response["success"] = 1;
        $response["user"]["pid"] = $user["pid"];
        $response["user"]["date_reg"] = $user["date_reg"];
        $response["user"]["name_first"] = $user["name_first"];
        $response["user"]["name_last"] = $user["name_last"];
        $response["user"]["nat_id_nr"] = $user["nat_id_nr"];
        $response["user"]["ethnic_orig"] = $user["ethnic_orig"];
        $response["user"]["date_birth"] = $user["date_birth"];
        $response["user"]["sex"] = $user["sex"];
        $response["user"]["blood_group"] = $user["blood_group"];
        $response["user"]["birth_country"] = $user["birth_country"];
        $response["user"]["birth_entity"] = $user["birth_entity"];
        $response["user"]["birth_town"] = $user["birth_town"];
        $response["user"]["birth_city"] = $user["birth_city"];
        $response["user"]["civil_status"] = $user["civil_status"];
        $response["user"]["illiterate"] = $user["illiterate"];
        $response["user"]["educational_level"] = $user["educational_level"];
        $response["user"]["educational_plan"] = $user["educational_plan"];
        $response["user"]["years_approved"] = $user["years_approved"];
        $response["user"]["profession"] = $user["profession"];
        $response["user"]["sso"] = $user["sso"];
        $response["user"]["addr_entity"] = $user["addr_entity"];
        $response["user"]["addr_citytown_name"] = $user["addr_citytown_name"];
        $response["user"]["addr_town"] = $user["addr_town"];
        $response["user"]["addr_locality"] = $user["addr_locality"];
        $response["user"]["addr_sector"] = $user["addr_sector"];
        $response["user"]["addr_str"] = $user["addr_str"];
        $response["user"]["addr_house"] = $user["addr_house"];
        $response["user"]["addr_floor"] = $user["addr_floor"];
        $response["user"]["addr_reference"] = $user["addr_reference"];
        $response["user"]["addr_time"] = $user["addr_time"];
        $response["user"]["name_mother"] = $user["name_mother"];
        $response["user"]["name_father"] = $user["name_father"];
        $response["user"]["create_id"] = $user["create_id"];

        echo json_encode($response);

    } else if ($tag == 'patBackFam') {
        $pid = $_POST['pid'];

        $user = $db->getPatientFamBack($pid);
        $response["success"] = 1;
        $i=0;
        for ($i=0; $i < count($user); $i=$i+2) {
          $response["user"][$user[$i]] = $user[$i+1];
        }

        /*
        $response["user"]["cancer"] = $user["cancer"];
        $response["user"]["cancer_txt"] = $user["cancer_txt"];
        */
        echo json_encode($response);

    } else if ($tag == 'patBackPer') {
        $pid = $_POST['pid'];

        $user = $db->getPatientPerBack($pid);
        $response["success"] = 1;
        $i=0;
        for ($i=0; $i < count($user); $i=$i+2) {
          $response["user"][$user[$i]] = $user[$i+1];
        }

        echo json_encode($response);

    } else if ($tag == 'patientImmu') {
        $pid = $_POST['pid'];

        $user = $db->getPatientImmu($pid);
        $response["success"] = 1;
        $response["user"]["antiamarilica"] = $user["antiamarilica"];
        $response["user"]["antihaemof_influenza_B"] = $user["antihaemof_influenza_B"];
        $response["user"]["antihepatitisB"] = $user["antihepatitisB"];
        $response["user"]["antiinfluenza"] = $user["antiinfluenza"];
        $response["user"]["antineumococcica"] = $user["antineumococcica"];
        $response["user"]["antineumococcica_BC"] = $user["antineumococcica_BC"];
        $response["user"]["antipoliomelitica"] = $user["antipoliomelitica"];
        $response["user"]["antirotavirus"] = $user["antirotavirus"];
        $response["user"]["bcg"] = $user["bcg"];
        $response["user"]["doble_viral"] = $user["doble_viral"];
        $response["user"]["otras"] = $user["otras"];
        $response["user"]["otras_especificacion"] = $user["otras_especificacion"];
        $response["user"]["pentavalente"] = $user["pentavalente"];
        $response["user"]["toxoide_difterico"] = $user["toxoide_difterico"];
        $response["user"]["toxoide_tetanico"] = $user["toxoide_tetanico"];
        $response["user"]["triple_bacteriana"] = $user["triple_bacteriana"];
        $response["user"]["trivalente_viral"] = $user["trivalente_viral"];

        echo json_encode($response);
    }
    /* else if ($tag == 'register') {
        // Request type is Register new user
        $name = $_POST['name'];
        $email = $_POST['email'];
        $password = $_POST['password'];

        // check if user is already existed
        if ($db->isUserExisted($email)) {
            // user is already existed - error response
            $response["error"] = 2;
            $response["error_msg"] = "User already existed";
            echo json_encode($response);
        } else {
            // store user
            $user = $db->storeUser($name, $email, $password);
            if ($user) {
                // user stored successfully
                $response["success"] = 1;
                $response["uid"] = $user["unique_id"];
                $response["user"]["name"] = $user["name"];
                $response["user"]["email"] = $user["email"];
                $response["user"]["created_at"] = $user["created_at"];
                $response["user"]["updated_at"] = $user["updated_at"];
                echo json_encode($response);
            } else {
                // user failed to store
                $response["error"] = 1;
                $response["error_msg"] = "Error occured in Registartion";
                echo json_encode($response);
            }
        }
    }*/ else {
        echo "Invalid Request";
    }
} else {
    echo "Access Denied ";
}
?>
